<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link href="/css/layui.css" rel="stylesheet">
<link href="/css/plugins/fileinput/fileinput.min.css" rel="stylesheet">
<style type="text/css">
    #kvFileinputModal {
        z-index: 99999999 !important;
    }
</style>
<body>
<div class="wrapper">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <input id="outChannel" name="outChannel" th:value="${payQrConfig.outChannel}" type="hidden">

                <input id="accountNo" name="accountNo" th:value="${payQrConfig.accountNo}" type="hidden">
                <div class="fixed-table-toolbar">
                    <div class="columns pull-left">
                        <button shiro:hasPermission="pay:payQrConfig:add" type="button" class="btn  btn-primary"
                                onclick="add()">
                            <i class="fa fa-plus" aria-hidden="true"></i>添加
                        </button>
                    </div>

                    <div class="columns pull-right">
                        <button class="btn btn-success" onclick="save()">保存</button>
                    </div>
                </div>
                <div id="divQrs" class="row col-sm-12">

                    <form class="form-horizontal" id="signupForm">
                        <tr th:each="qrsDO,itemValue : ${qrList}">

                            <div class="form-group">
                                <label class="col-sm-2 control-label">金额（￥）：</label>
                                <div class="col-sm-2">
                                    <input type="number" th:id="money+(${itemValue.count})" class="form-control"
                                           max="50000" onkeypress="return
							noNumbers(event)" min="1" th:value="${qrsDO.money}">
                                </div>
                                <label class="col-sm-2 control-label">二维码字符串：</label>
                                <div class="col-sm-2">
                                    <input th:id="qrs+(${itemValue.count})" th:name="qrs+(${itemValue.count})"
                                           class="form-control"
                                           type="text" maxlength="1024" th:value="${qrsDO.qrs}">
                                </div>
                                <a href="#" class="removeclass" th:value="${itemValue.count}">×</a>
                            </div>
                        </tr>
                    </form>
                </div>
            </div>

        </div>
    </div>
</div>
</div>
<script type="text/javascript" src="/js/jquery.min.js"></script>

<script th:inline="javascript">

    var FieldCount =parseInt([[${num}]]) ; //to keep track of text box added
    var removeNum = [];
    $().ready(function () {
        $("body").on("click", ".removeclass", function (e) { //user click on remove text

            removeNum.push($(this).attr("value"));

            $(this).parent('div').remove(); //remove text box

            return false;
        })
    });

    function add() {

        FieldCount++; //text box added increment
        //add input box
        $(signupForm).append('<div class="form-group">'
            + '  <label class="col-sm-2 control-label">金额（￥）：</label>'
            + '<div class="col-sm-2">'
            + '<input type="number" id="money' + FieldCount + '" class="form-control" max="100000" onkeypress="return'
            + ' noNumbers(event)" min="1">'
            + '   </div>'
            + '  <label class="col-sm-2 control-label">二维码字符串：</label>'
            + '  <div class="col-sm-2">'
            + '<input id="qrs' + FieldCount + '" name="qrs' + FieldCount + '"  class="form-control"'
            + 'type="text" maxlength="1024">'
            + ' </div><a href="#" class="removeclass" value="' + FieldCount + '" >×</a></div>'
            + '</div>');

    }

    function save() {

        var useNum = [];
        for (var i = 1; i <= FieldCount; i++) {
            var flag = false;
            for (var j = 0; j < removeNum.length; j++) {
                if (removeNum[j] == i) {
                    flag = true;
                    continue;
                }
            }
            if (flag) {
                continue;
            }
            useNum.push(i);
        }
        var moneyarray = [];
        if(useNum.length==0){
            parent.layer.alert("至少选择填写一个金额");
            return;
        }
        for (var i = 0; i < useNum.length; i++) {
            var moneyJson = {};
            moneyJson["money"] = $("#money" + useNum[i]).val();
            moneyJson["qrs"] = $("#qrs" + useNum[i]).val();
            moneyarray.push(moneyJson);
        }
        var postData = {};
        postData["qrs"] = JSON.stringify(moneyarray);
        postData["outChannel"] = $("#outChannel").val();
        postData["accountNo"] = $("#accountNo").val();
        // alert(JSON.stringify(moneyJson))
        $.ajax({
            cache: true,
            type: "POST",
            url: "/pay/payQrConfig/updateQrs",
            data: postData,// 你的formid
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.code == 0) {
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }

            }
        });

    }

    //只能输入两位小数
    function noNumbers(e) {
        var keynum
        var keychar
        var numcheck
        if (window.event) { // IE
            keynum = e.keyCode
        } else if (e.which) {// Netscape/Firefox/Opera
            keynum = e.which
        }
        keychar = String.fromCharCode(keynum);
        // 判断是数字,且小数点后面只保留两位小数
        if (!isNaN(keychar)) {
            var index = e.currentTarget.value.indexOf(".");
            if (index >= 0 && e.currentTarget.value.length - index > 2) {
                return false;
            }
            return true;
        }
        // 如果是小数点 但不能出现多个 且第一位不能是小数点
        if ("." == keychar) {
            if (e.currentTarget.value == "") {
                return false;
            }
            if (e.currentTarget.value.indexOf(".") >= 0) {
                return false;
            }
            return true;
        }
        return false;
    }
</script>
<script type="text/javascript" src="/js/md5.min.js"></script>
<script type="text/javascript" src="/js/plugins/fileinput/fileinput.min.js"></script>
<script type="text/javascript" src="/js/layui.js"></script>
<script type="text/javascript" src="/js/appjs/pay/payQrConfig/editQr.js"></script>
<script type="text/javascript" src="/js/plugins/fileinput/locales/zh.js"></script>
</body>
</html>
